package javaclientcp3110.View.Component;
//CategoryListView and ComponentListView are required, so i added them here just
//in case the file is moved so that refactor will keep this import.

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import common.Objects.Category;
import common.Objects.Component;
import javaclientcp3110.Model.Component.CompEditModel;
import javaclientcp3110.Model.MainModel;
import common.UCM.UC2AL;
import javaclientcp3110.UCM.UCEditComponent;
import javax.swing.JOptionPane;


/*
 * CompEditView.java
 *
 * Created on 14/08/2009, 4:02:46 PM
 * View created through generator. This is the view for the edit view.
 * @author Richard Irons
 */
public class CompEditView extends javax.swing.JPanel {

    private CompEditModel model;
    /** Creates new form CompEditView */
    public CompEditView() {
        this(new CompEditModel());
    }

    public CompEditView(CompEditModel model) {
        this.model = model;        
        
        initComponents();

        compEditButton.addActionListener(new UC2AL(new UCEditComponent(this, model)));
        
        Category currentItem = (Category)compCatagoryComboBox.getSelectedItem();
        componentComboBox.populateComboBox_viaCategory(currentItem.getId());
        
        updateCurrentComponent();

        compCatagoryComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                Category currentItem = (Category) e.getItem();
                componentComboBox.populateComboBox_viaCategory(currentItem.getId());
            }
        });

        componentComboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                updateCurrentComponent();
            }
        });
    }
    public String getCompName(){return compNameTextField.getText();}
    public String getCompDescription(){return compDescriptionTextField.getText();}
    public int getCompCurrentStock(){return Integer.parseInt(compStockTextField.getText());}
    public double getCompPrice(){return Double.parseDouble(compPriceTextField.getText());}
    public Category getCompCategory(){return (Category) compCatagoryComboBox.getSelectedItem();}
    public int getCompId() {
        if (componentComboBox.getItemCount() > 0) {
            Component c = (Component) componentComboBox.getSelectedItem();
            return c.getId();
        }
        else {
            return -1;
        }
    }

    private void updateCurrentComponent() {
        if (componentComboBox.getItemCount() > 0) {
            Component currentItem = (Component)componentComboBox.getSelectedItem();
            compNameTextField.setText(currentItem.getName());
            compDescriptionTextField.setText(currentItem.getDescription());
            compPriceTextField.setText(String.valueOf(currentItem.getPrice()));
            compStockTextField.setText(String.valueOf(currentItem.getCurrentStock()));
        }
        else {
            compNameTextField.setText("");
            compDescriptionTextField.setText("");
            compPriceTextField.setText("");
            compStockTextField.setText("");
        }
    }
    public void errorMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Error",
            JOptionPane.ERROR_MESSAGE);
    }
    public void successMessage(String message) {
        JOptionPane.showMessageDialog(null,
            message,
            "Success!",
            JOptionPane.INFORMATION_MESSAGE);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        compNameTextField = new javax.swing.JTextField();
        compPriceLabel = new javax.swing.JLabel();
        compStockLabel = new javax.swing.JLabel();
        compDescriptionLabel = new javax.swing.JLabel();
        compNameLable = new javax.swing.JLabel();
        compEditButton = new javax.swing.JButton();
        titleLabel = new javax.swing.JLabel();
        compDescriptionTextField = new javax.swing.JTextField();
        compPriceTextField = new javax.swing.JTextField();
        compStockTextField = new javax.swing.JTextField();
        compCatagoryLabel = new javax.swing.JLabel();
        compComponentLabel = new javax.swing.JLabel();
        componentComboBox = new javaclientcp3110.View.Component.ComponentListView();
        compCatagoryComboBox = new javaclientcp3110.View.Component.CategoryListView(MainModel.getInstance().getCategorys());

        setMaximumSize(new java.awt.Dimension(500, 380));
        setMinimumSize(new java.awt.Dimension(500, 380));

        compNameTextField.setColumns(25);
        compNameTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        compPriceLabel.setText("Price:");

        compStockLabel.setText("Current Stock:");

        compDescriptionLabel.setText("Description:");

        compNameLable.setText("Name:");

        compEditButton.setText("Edit Component");
        compEditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compEditButtonActionPerformed(evt);
            }
        });

        titleLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        titleLabel.setText("Select a component to edit then change the details");

        compDescriptionTextField.setColumns(25);
        compDescriptionTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        compPriceTextField.setColumns(25);
        compPriceTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        compStockTextField.setColumns(25);
        compStockTextField.setMaximumSize(new java.awt.Dimension(6, 20));

        compCatagoryLabel.setText("Catagory:");

        compComponentLabel.setText("Component:");

        componentComboBox.setMaximumSize(new java.awt.Dimension(28, 20));

        compCatagoryComboBox.setMaximumSize(new java.awt.Dimension(28, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(titleLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(compNameLable)
                            .addComponent(compPriceLabel)
                            .addComponent(compDescriptionLabel)
                            .addComponent(compStockLabel)
                            .addComponent(compComponentLabel)
                            .addComponent(compCatagoryLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(componentComboBox, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(compPriceTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(compStockTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(compDescriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(compNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(compCatagoryComboBox, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(compEditButton, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)))
                .addGap(95, 95, 95))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(titleLabel)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compCatagoryLabel)
                    .addComponent(compCatagoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compComponentLabel)
                    .addComponent(componentComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(compNameLable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compDescriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(compDescriptionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compStockTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(compStockLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compPriceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(compPriceLabel))
                .addGap(18, 18, 18)
                .addComponent(compEditButton)
                .addContainerGap(105, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void compEditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compEditButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_compEditButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javaclientcp3110.View.Component.CategoryListView compCatagoryComboBox;
    private javax.swing.JLabel compCatagoryLabel;
    private javax.swing.JLabel compComponentLabel;
    private javax.swing.JLabel compDescriptionLabel;
    private javax.swing.JTextField compDescriptionTextField;
    private javax.swing.JButton compEditButton;
    private javax.swing.JLabel compNameLable;
    private javax.swing.JTextField compNameTextField;
    private javax.swing.JLabel compPriceLabel;
    private javax.swing.JTextField compPriceTextField;
    private javax.swing.JLabel compStockLabel;
    private javax.swing.JTextField compStockTextField;
    private javaclientcp3110.View.Component.ComponentListView componentComboBox;
    private javax.swing.JLabel titleLabel;
    // End of variables declaration//GEN-END:variables

}
